package com.eatrip.dao.implementation;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.eatrip.bean.Provincia;
import com.eatrip.dao.ProvinceDAO;

@Repository
@Transactional
public class ProvinceDAOImple implements ProvinceDAO {
	
	private HibernateTemplate hibernateTemplate;
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		hibernateTemplate = new HibernateTemplate(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public Provincia getProvincia(int id) {
		List<Provincia> list = hibernateTemplate.find("FROM Provincia WHERE id=?",id);
		if (list.size() == 0)
			return null;
		return list.get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Provincia> getAllProvince(int start, int numRow) {
		Criteria criteria = hibernateTemplate.getSessionFactory().getCurrentSession().createCriteria(Provincia.class);
			criteria.setFirstResult(start);
			criteria.setMaxResults(numRow);
		List<Provincia> list = criteria.list();
		if (list.size() == 0)
			return null;
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Provincia> getProvinceByIdRegione(int idRegione) {
		List<Provincia> list = hibernateTemplate.find("FROM Provincia WHERE codiceRegione=?",idRegione);
		if (list.size() == 0)
			return null;
		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Provincia> getProvinceByNomeRegione(String nome) {
		List<Provincia> list = hibernateTemplate.find("FROM Provincia WHERE nome=?",nome);
		if (list.size() == 0)
			return null;
		return list;
	}

}
